package org.jhotdraw.draw.action;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;

import org.jhotdraw.draw.Drawing;
import org.jhotdraw.draw.DrawingEditor;
import org.jhotdraw.draw.DrawingView;
import org.jhotdraw.draw.Figure;
import org.jhotdraw.draw.ImageFigure;
import org.jhotdraw.util.ResourceBundleUtil;

public class ConvertToGrayScaleAction extends AbstractSelectedAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public final static String ID = "edit.convertToGrayScale";

	/** Creates a new instance. */
	public ConvertToGrayScaleAction(DrawingEditor editor) {
		super(editor);
		ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.draw.Labels");
		labels.configureAction(this, ID);
		updateEnabledState();
	}

	@Override
	public void actionPerformed(java.awt.event.ActionEvent e) {
		final DrawingView view = getView();
		final LinkedList<Figure> figures = new LinkedList<Figure>(view.getSelectedFigures());
		convertToGrayScale(view, figures);
	}

	public void convertToGrayScale(DrawingView view, Collection<Figure> figures) {
		Drawing drawing = view.getDrawing();
		Iterator i = drawing.sort(figures).iterator();
		while (i.hasNext()) {
			Figure figure = (Figure) i.next();
			if (figure instanceof ImageFigure){
				ImageFigure image = (ImageFigure) figure;
				image.makeGray();
			}
		}
		
	}
}

